'use strict';

const fs = require('fs');

module.exports = appInfo => {
    const config = {};

    // use for cookie sign key, should change to your own and keep security
    config.keys = appInfo.name + '_1525787551528_2099';

    config.security = {
        csrf: {
            enable: true,
            ignore: '/api',
            queryName: '_csrf',
            bodyName: '_csrf',
        },
        domainWhiteList: [
            'localhost:8080',
            'localhost:9528',
            'localhost:9215',
        ]
    };

    exports.cors = {
        origin: '*',
        allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
    };

    config.nunjucks = {
        enable: true,
        package: 'egg-view-nunjucks'
    };

    config.view = {
        defaultViewEngine: 'nunjucks',
        mapping: {
            '.html': 'nunjucks',
        },
    };

    config.mysql = {
        client: {
            host: 'localhost',
            port: '3306',
            user: 'root',
            password: 'maje0245',
            database: 'eden_database',
        },
    };

    config.sequelize = {
        dialect: 'mysql',
        database: 'eden_database',
        host: 'localhost',
        port: '3306',
        username: 'root',
        password: 'maje0245',
    };

    config.redis = {
        client: {
            port: 6379,
            host: '127.0.0.1',
            password: 'maje0245',
            db: 0,
        },
    };

    exports.oAuth2Server = {
        debug: config.env === 'local',
        grants: [ 'password' ],
    };

    exports.multipart = {
        fileSize: '10mb',
        whitelist: [
            '.jpg', '.jpeg', // image/jpeg
            '.png', // image/png, image/x-png
            '.gif', // image/gif
            '.bmp', // image/bmp
            '.wbmp', // image/vnd.wap.wbmp
            '.webp',
            '.tif',
            '.psd',
            // text
            '.svg',
            '.js', '.jsx',
            '.json',
            '.css', '.less',
            '.html', '.htm',
            '.xml',
            // tar
            '.zip',
            '.gz', '.tgz', '.gzip',
            // video
            '.mp3',
            '.mp4',
            '.avi',
        ]
    };

    // add your config here
    config.middleware = [
        'authHandler',
        'errorHandler',
        'report'
    ];

    config.errorHandler = {
        enable: true,
        match: '/api',
        title: 'error handler'
    };

    config.authHandler = {
        enable: true,
        match: (ctx) => {
            const app = /app/;
            const pub = /pub/;
            return !(pub.test(ctx.url) || app.test(ctx.url));
        },
        title: 'auth handler'
    };

    config.report = {
        enable: true,
        match: '/api/news',
        title: 'report test'
    };

    return config;
};
